﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WAWrapper;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            //Get your key from https://developer.wolframalpha.com/portal/myapps/
            var wa = new WAEngine { APIKey = "YOUR KEY HERE" };
            WALogger.LogLevel = WALogLevel.None;
            WALogger.ConsoleLogLevel = WALogLevel.Verbose;
            var question = Console.ReadLine();
            var query = new WAQuery() {Input = question, Format = WAQueryFormat.PlainText};
            query.PodStates.Add("test");
            query.AppID = wa.APIKey;
            string url = query.FormatQuery();
            var result = wa.RunQuery(query);
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("Result found. {0} seconds to parse query, {1} seconds to get the result",result.ParseTiming, result.Timing);
            if (result.TimedOutPods.Count > 0)
                Console.WriteLine("Timed out pods: {0}",result.TimedOutPods.Aggregate((current, s) => current +"," + s));
            
            foreach (var pod in result.Pods)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine(pod.Title);
                
                foreach (var subpod in pod.SubPods)
                {
                    Console.ForegroundColor = ConsoleColor.White;
                    if (!string.IsNullOrEmpty(subpod.Title))
                    {
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        Console.WriteLine("Title: " + subpod.Title);
                    }
                    if (subpod.Image != null)
                    {
                        Console.WriteLine("Image: " + subpod.Image.URL);
                    }
                    if (!string.IsNullOrEmpty(subpod.PlainText))
                    {
                        Console.ForegroundColor = ConsoleColor.White;
                        Console.WriteLine(subpod.PlainText);
                    }

                }
            }
            foreach (var a in result.Assumptions)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Assumption Type: {0}, Word: {1}, Template: {2}",a.AssumptionType,a.Word,a.Template);
                Console.ForegroundColor = ConsoleColor.White;
                foreach (var v in a.Values)
                {
                    Console.WriteLine("Name: {0}, Description: {1}, Input: {2}", v.Name, v.Description, v.Input);
                }
            }

            if (result.Sources.Count > 0)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Sources");
                Console.ForegroundColor = ConsoleColor.White;
                foreach (var s in result.Sources)
                {
                    Console.WriteLine(s.Text + ": " + s.URL);
                }
            }


            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Complete!");
            Console.Read();

        }
    }
}
